<?php
/**
 * DeducteModel.class.php
 * 利益分成模型
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-03-02
 */
namespace Admin\Model;

class DeductModel extends BaseModel {

	protected 	$trueTableName 	= 't_deduct';

	/**
	 * 详细利益分成信息
	 * @param   $moneyId 支付ID
	 * @return  $list [<description>]
	 */
	function detailInfo($moneyId){
		$list 	= $this->table('t_deduct a')
			->join('left join t_user b on a.user_id = b.id')
			->where('a.money_id = %d', $moneyId)
			->field('a.user_id, a.money_id, a.amount, a.percent, a.create_time,
				b.nickname, b.truename, b.role_id')
			->cache(true)
			->select();

		foreach($list as &$item){
			$item['role_text']	= FUserModel::ROLE_MAP[$item['role_id']];
		}

		return $list;
	}

	/**
	 * 获取资金分配列表
	 * @param   $page 页码数
	 * @param   $keyword 关键词
	 * @param   $timeS 起始时间
	 * @param   $timeE 结束时间
	 * @param   $pageHtml 引用页面HTML分页元素
	 * @return  $list [<description>]
	 */
	function getList($keyword, $timeS, $timeE, $page, &$pageHtml, &$total){
		$where 	= '1 = 1';
		if($keyword)
			$where 	.= sprintf(' and (b.nickname like "%%%s%%" or b.id = "%s")', $keyword, $keyword);
		if($timeS)
			$where 	.= sprintf(' and a.create_time >= %d', $timeS);
		if($timeE)
			$where 	.= sprintf(' and a.create_time <= %d', $timeE);

		$list 	= $this->table('t_deduct a')
			->join('inner join t_user b on a.user_id = b.id')
			->join('inner join t_money c on a.money_id = c.id')
			->where($where)
			->order('b.role_id')
			->field('a.id, a.money_id, a.user_id, a.amount get_amount, a.percent, a.create_time,
				b.nickname, b.truename, b.role_id, c.amount, c.trade_no, c.goods_type, c.deduct')
			->cache(true)
			->page($page, C('PAGE_COUNT'))
			->select();

		$list 	= $this->_analyField('deduct', $list, true);

		//分页HTML获取
		if($pageHtml){
			$count 	= $this->table('t_deduct a')
				->join('left join t_user b on a.user_id = b.id')
				->where($where)
				->cache(true)
				->count();

			$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
			$pageHtml 	= $Page->angular_show();
		}

		//合计信息获取
		if($total){
			$total 	= $this->table('t_deduct a')
				->join('left join t_user b on a.user_id = b.id')
				->join('left join t_money c on a.money_id = c.id')
				->where($where)
				->cache(true)
				->field('count(a.id) count, sum(a.amount) get_amount, sum(c.amount) amount')
				->find();
		}
			
		return $list;
	}


	/**
	 * 导出Excel
	 * @param   $start 	开始位置
	 * @param   $lenght 到处长度
	 * @param   $keyword 关键词
	 * @param   $timeS 起始时间
	 * @param   $timeE 结束时间
	 * @return  list [<description>]
	 */
	function outExcel($start, $length, $keyword, $timeS, $timeE){
		$where 	= '1 = 1';
		if($keyword)
			$where 	.= sprintf(' and (b.nickname like "%s%%" or b.id = "%s")', $keyword, $keyword);
		if($timeS)
			$where 	.= sprintf(' and a.create_time >= %d', $timeS);
		if($timeE)
			$where 	.= sprintf(' and a.create_time <= %d', $timeE);

		if($start === false){
			$count 	= $this->table('t_deduct a')
				->join('left join t_user b on a.user_id = b.id')
				->where($where)
				->cache(true)
				->count();

			return $count;
		}

		$list 	= $this->table('t_deduct a')
			->join('left join t_user b on a.user_id = b.id')
			->join('left join t_money c on a.money_id = c.id')
			->where($where)
			->order('id desc')
			->field('a.id, a.money_id, a.user_id, a.amount get_amount, a.percent, a.create_time,
				b.nickname, b.truename, b.role_id, c.amount, c.trade_no, c.goods_type, c.deduct')
			->cache(true)
			->limit($start, $length)
			->select();

		$roleMap 	= \Common\Model\UserModel::ROLE_MAP;
		foreach($list as &$item){
			$item['create_date']	= date('Y-m-d', $item['create_time']);
			$item['goods_type_text']	= C('MONEY_GOODS_TYPE.'.$item['goods_type']);
			$item['role_text']	= $roleMap[$item['role_id']];
			$item['profit']	= $item['amount'] - $item['deduct'];
		}
		return $list;	
	}

	/**
	 * 获取直属下线分红记录
	 * @param 	$keyword
	 * @param 	$timeS
	 * @param 	$timeE
	 * @param 	$page
	 * @param 	&$pageHtml
	 * @param 	&$total
	 * @return 	list
	 */
	function getExpandList($keyword, $timeS, $timeE, $page, &$pageHtml, &$total){
		$where 	= 'b.role_id >= 102 and b.role_id <= 104 and d.dean_id = -1';
		if($keyword)
			$where 	.= sprintf(' and (b.nickname like "%%%s%%" or b.id = "%s")', $keyword, $keyword);
		if($timeS)
			$where 	.= sprintf(' and a.create_time >= %d', $timeS);
		if($timeE)
			$where 	.= sprintf(' and a.create_time <= %d', $timeE);

		$list 	= M('t_deduct a', null)
			->join('inner join t_user b on a.user_id = b.id')
			->join('inner join t_money c on a.money_id = c.id')
			->join('inner join t_user_relative d on a.user_id = d.user_id')
			->where($where)
			->field('a.id, a.money_id, a.user_id, a.amount get_amount, a.percent, a.create_time,
				b.nickname, b.truename, b.role_id, c.amount, c.trade_no, c.goods_type, c.deduct')
			->cache(true)
			->page($page, C('PAGE_COUNT'))
			->select();
		$list 	= $this->_analyField('deduct', $list, true);

		//分页HTML获取
		if($pageHtml){
			$count 	= $this->table('t_deduct a')
				->join('inner join t_user b on a.user_id = b.id')
				->join('inner join t_user_relative d on a.user_id = d.user_id')
				->where($where)
				->cache(true)
				->count();

			$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
			$pageHtml 	= $Page->angular_show();
		}

		//合计信息获取
		if($total){
			$total 	= $this->table('t_deduct a')
				->join('inner join t_user b on a.user_id = b.id')
				->join('inner join t_money c on a.money_id = c.id')
				->join('inner join t_user_relative d on a.user_id = d.user_id')
				->where($where)
				->cache(true)
				->field('count(a.id) count, sum(a.amount) get_amount, sum(c.amount) amount')
				->find();
		}
			
		return $list;

	}

	/**
	 * 获取下级用户分红
	 * @param 	$fatherId 	院长ID
	 * @param 	$page
	 * @param 	&$pageHtml
	 * @return 	$list
	 */
	function getChildList($fatherId, $page, &$pageHtml){
		$list 	= M('t_deduct as a', null)
			->join('left join t_user as b on a.user_id = b.id')
			->join('left join t_money as c on a.money_id = c.id')
			->join('left join t_user_relative as d on a.user_id = d.user_id')
			->where('d.dean_id = %d', $fatherId)
			->order('a.id desc')
			->field('a.id, a.money_id, a.user_id, a.amount get_amount, a.percent, a.create_time,
				b.nickname, b.truename, b.role_id, c.amount, c.trade_no, c.goods_type, c.deduct')
			->cache(true)
			->select();

		$list 	= $this->_analyField('deduct', $list, true);

		if($pageHtml === true){
			$count 	= M('t_deduct as a', null)
				->join('left join t_user as b on a.user_id = b.id')
				->join('left join t_money as c on a.money_id = c.id')
				->join('left join t_user_relative as d on a.user_id = d.user_id')
				->where('d.dean_id = %d', $fatherId)
				->cache(true)
				->count();

			$Page 	= new \Think\Page($count, C('PAGE_COUNT'));
			$pageHtml 	= $Page->angular_show();
		}

		return $list;
	}
}